package controller

import (
	models "devops-dcc/internal/model"
	"devops-dcc/internal/services"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type PlatformController struct {
	service *services.PlatformService
}

func NewPlatformController(service *services.PlatformService) *PlatformController {
	return &PlatformController{service: service}
}

// 平台CRUD
func (c *PlatformController) CreatePlatform(ctx *gin.Context) {
	var req models.CreatePlatformRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	platform, err := c.service.CreatePlatform(req)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusCreated, platform)
}

func (c *PlatformController) GetPlatform(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid platform id"})
		return
	}

	platform, err := c.service.GetPlatform(uint(id))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, platform)
}

func (c *PlatformController) GetAllPlatforms(ctx *gin.Context) {
	platforms, err := c.service.GetAllPlatforms()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, platforms)
}

func (c *PlatformController) UpdatePlatform(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid platform id"})
		return
	}

	var req models.UpdatePlatformRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	platform, err := c.service.UpdatePlatform(uint(id), req)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, platform)
}

func (c *PlatformController) DeletePlatform(ctx *gin.Context) {
	id, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid platform id"})
		return
	}

	if err := c.service.DeletePlatform(uint(id)); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{"message": "platform deleted"})
}

// 环境CRUD
func (c *PlatformController) CreateEnvironment(ctx *gin.Context) {
	var req models.CreateEnvironmentRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	env, err := c.service.CreateEnvironment(req)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusCreated, env)
}

func (c *PlatformController) GetAllEnvironments(ctx *gin.Context) {
	envs, err := c.service.GetAllEnvironments()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, envs)
}

// 平台环境配置
func (c *PlatformController) SetPlatformEnvConfig(ctx *gin.Context) {
	platformID, err := strconv.Atoi(ctx.Param("platform_id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid platform id"})
		return
	}

	envID, err := strconv.Atoi(ctx.Param("environment_id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid environment id"})
		return
	}

	var req models.PlatformEnvConfigRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err := c.service.SetPlatformEnvConfig(uint(platformID), uint(envID), req.Config); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{"message": "config updated"})
}

func (c *PlatformController) GetPlatformEnvConfig(ctx *gin.Context) {
	platformID, err := strconv.Atoi(ctx.Param("platform_id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid platform id"})
		return
	}

	envID, err := strconv.Atoi(ctx.Param("environment_id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid environment id"})
		return
	}

	config, err := c.service.GetPlatformEnvConfig(uint(platformID), uint(envID))
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusOK, config)
}
